package com.aisino.nmg.task;

import com.aisino.nmg.ant.GeneratorUtil;
import com.aisino.nmg.ant.env.Environment;
import com.aisino.nmg.ant.task.AbstractTask;
import com.aisino.nmg.ant.task.GeneratorToolTask;
import com.aisino.nmg.constant.GeneratorConstants;
import com.aisino.nmg.system.generator.model.AppConfig;
import com.aisino.nmg.system.generator.model.ColumnConfig;
import com.aisino.nmg.system.generator.model.TableConfig;
import com.aisino.nmg.template.EngineBuilder;
import com.aisino.nmg.template.TemplateEngine;
import com.aisino.nmg.util.FileUtil;
import com.aisino.nmg.util.PathUtil;
import com.aisino.nmg.util.StringUtil;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GeneratorDaoCodeTask extends AbstractTask {
	private String modelSourcePath;
	private String mapperSourcePath;

	public GeneratorDaoCodeTask(GeneratorToolTask parent) {
		super(parent);
	}

	@Override
	public String getName() {
		return "SSM 数据操作模块代码生成";
	}

	@Override
	public Integer getSort() {
		return new Integer(1);
	}

	@Override
	public void execute() {
		Environment environment = this.getParent().getEnvironment();
		String PROJECT_ROOT_DIR = environment.getGeneratorDir() + this.getParent().getProject().getArtifactId();
		mapperSourcePath = PROJECT_ROOT_DIR + File.separator + environment.getSubDaoProjectName() + GeneratorConstants.JAVA_SOURCE_PATH + StringUtil.packagePathToFilePath(this.getParent().getProject().getGroupId() + GeneratorConstants.MAPPER_PACKAGE);
		modelSourcePath = PROJECT_ROOT_DIR + File.separator + environment.getSubDaoProjectName() + GeneratorConstants.JAVA_SOURCE_PATH + StringUtil.packagePathToFilePath(this.getParent().getProject().getGroupId() + GeneratorConstants.MODEL_PACKAGE);
		AppConfig appConfig = environment.getAppconfig();
		List<TableConfig> tableConfigs = this.getParent().getTableConfigMapper().selectByAppConfigId(appConfig.getId());
		Iterator<TableConfig> it = tableConfigs.iterator();
		while(it.hasNext()){
			TableConfig tableConfig = it.next();
			List<ColumnConfig> columnConfigs = this.getParent().getColumnConfigMapper().selectByTableConfigId(tableConfig.getId());
			generatorModelCode(appConfig, tableConfig, columnConfigs);
			generatorMapperCode(appConfig, tableConfig, columnConfigs);
		}
	}

	private void generatorMapperCode(AppConfig appConfig, TableConfig tableConfig, List<ColumnConfig> columnConfigs){
		EngineBuilder engineBuilder = new EngineBuilder(PathUtil.getCurrentProjectPath() + GeneratorConstants.RESOURCES_SOURCE_PATH);
		TemplateEngine templateEngine = engineBuilder.getTemplateEngine("freemarker");
		FileUtil.createFilePath(mapperSourcePath, tableConfig.getDomainObjectName() + GeneratorConstants.MAPPER_DOMAIN);
		Map<String, Object> model = new HashMap<>();
		model.put("appConfig", appConfig);
		model.put("tableConfig", tableConfig);
		model.put("columnConfigs", columnConfigs);
		model.put("primaryKey", GeneratorUtil.getPrimaryKey(columnConfigs));

		templateEngine.process(model, "ssm" + File.separator + "dao" + File.separator + "dao-mapper-java.ftl",
		                       FileUtil.writeFileToPath(mapperSourcePath, tableConfig.getDomainObjectName() + GeneratorConstants.MAPPER_DOMAIN));
	}

	private void generatorModelCode(AppConfig appConfig, TableConfig tableConfig, List<ColumnConfig> columnConfigs){
		EngineBuilder engineBuilder = new EngineBuilder(PathUtil.getCurrentProjectPath() + GeneratorConstants.RESOURCES_SOURCE_PATH);
		TemplateEngine templateEngine = engineBuilder.getTemplateEngine("freemarker");
		FileUtil.createFilePath(modelSourcePath, tableConfig.getDomainObjectName() + GeneratorConstants.JAVA_DOMAIN);
		Map<String, Object> model = new HashMap<>();
		model.put("appConfig", appConfig);
		model.put("tableConfig", tableConfig);
		model.put("columnConfigs", columnConfigs);

		templateEngine.process(model, "ssm" + File.separator + "dao" + File.separator + "dao-model-java.ftl",
		                       FileUtil.writeFileToPath(modelSourcePath, tableConfig.getDomainObjectName() + GeneratorConstants.JAVA_DOMAIN));
	}
}
